package com.gas.project.gas.teacher.service.impl;

import java.util.List;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.text.Convert;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gas.project.gas.teacher.mapper.TeacherMapper;
import com.gas.project.gas.teacher.domain.Teacher;
import com.gas.project.gas.teacher.service.ITeacherService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教职工信息Service业务层处理
 *
 * @author xyl
 * @date 2022-04-02
 */
@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private IUserService userService;

    /**
     * 查询教职工信息
     *
     * @param teacherNumber 教职工信息ID
     * @return 教职工信息
     */
    @Override
    public Teacher selectTeacherById(String teacherNumber) {
        return teacherMapper.selectTeacherById(teacherNumber);
    }

    /**
     * 查询教职工信息列表
     *
     * @param teacher 教职工信息
     * @return 教职工信息
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教职工信息
     *
     * @param teacher 教职工信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTeacher(Teacher teacher) {
        teacher.setCreateTime(DateUtils.getNowDate());
        userService.insertUser(loadAddTeacherToUser(teacher));
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教职工信息
     *
     * @param teacher 教职工信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTeacher(Teacher teacher) {
        teacher.setUpdateTime(DateUtils.getNowDate());
        int i = teacherMapper.updateTeacher(teacher);


        User temp = userService.selectUserByLoginName(teacher.getTeacherNumber());
        User user = loadAddTeacherToUser(teacher);
        user.setUserId(temp.getUserId());
        int i1 = userService.updateUserInfo(user);
        return i + i1;
    }

    /**
     * 删除教职工信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTeacherByIds(String ids) {
        userService.deleteUserByLoginNames(Convert.toStrArray(ids));
        return teacherMapper.deleteTeacherByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除教职工信息信息
     *
     * @param teacherNumber 教职工信息ID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTeacherById(String teacherNumber) {
        userService.deleteUserByLoginName(teacherNumber);
        return teacherMapper.deleteTeacherById(teacherNumber);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String importTeacher(List<Teacher> teacherList) {
        for (Teacher teacher : teacherList) {
            userService.insertUser(loadAddTeacherToUser(teacher));
            teacherMapper.insertTeacher(teacher);
        }
        return "导入成功！";
    }


    private User loadAddTeacherToUser(Teacher teacher){
        User user = new User();
        BeanUtils.copyProperties(teacher,user);
        user.setLoginName(teacher.getTeacherNumber());
        user.setPassword("123456");
        user.setRoleId(2L);
        user.setUserName(teacher.getTeacherName());
        user.setSex(StringUtils.equals(teacher.getGender(),"女") ? "1" : "0");
        user.setUserType("00");
        user.setCreateTime(DateUtils.getNowDate());
        user.setCreateBy(teacher.getCreateBy());
        return user;
    }

    private User loadUpdateTeacherToUser(Teacher teacher){
        User user = new User();
        BeanUtils.copyProperties(teacher,user);
        user.setUserName(teacher.getTeacherNumber());
        user.setUpdateBy(teacher.getUpdateBy());
        user.setSex(StringUtils.equals(teacher.getGender(),"女") ? "1" : "0");
        user.setUpdateTime(DateUtils.getNowDate());
        return user;
    }
}
